﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudTree.Filters;
using System.Net;
using System.IO;
using System.Net.Http;
using WebMatrix.WebData;
using CloudTree.Models;
using DotNetOpenAuth.OAuth2;
using System.Collections.Specialized;

namespace CloudTree.Controllers
{
    [InitializeSimpleMembership]
    public class BoxLinkController : Controller
    {
        #region constants
        private const string JsonFormat =
            @"{{
                ""ClientId"": ""{0}"",
                ""ClientSecret"": ""{1}"",
                ""AccessToken"": ""{2}"",
                ""RefreshToken"": ""{3}"",
            }}";

        private const string ClientId = "xgajd1cijv5ouqgjh8fo8xqe34sxg3te";
        private const string ClientSecret = "62PA2IU2jGfiCDM7XZ13aPXFYJbtETbM";
        private const string AccessToken = "accessToken";
        private const string RefreshToken = "refreshToken";
        #endregion

        private ActionResult Start()//Reset the critical values to default
        {
            ClearSession();
            return View();
        }

        private void ClearSession()//Called by Start()
        {
            Session[ClientId] = null;
            Session[ClientSecret] = null;
            Session[AccessToken] = null;
            Session[RefreshToken] = null;
        }

        [HttpGet]
        public ActionResult BoxOAuth2Request()
        {
            String url = "https://www.box.com/api/oauth2/authorize?response_type=code&client_id=xgajd1cijv5ouqgjh8fo8xqe34sxg3te&state=authenticated";
            HttpWebRequest requestPermission = (HttpWebRequest)WebRequest.Create(url);
            requestPermission.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)requestPermission.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
       
            ContentResult cr = new ContentResult();
            cr.Content = sr.ReadToEnd();

            //PLease remember to continue working on this section

            return cr;//the uri of the request permission page
        }
    }
}
        /*
        [HttpPost]
        public ActionResult BoxOAuth2Request(bool request)
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("grant_type", "authorization_code");
            parameters.Add("code", "392621gfdlfj2k2hf7g2lfhj2g");
            parameters.Add("client_id", ClientId);
            parameters.Add("client_secret", ClientSecret);

            WebClient client = new WebClient();
            var result = client.UploadValues("https://www.box.com/api/oauth2/token", parameters);

            var response = System.Text.Encoding.Default.GetString(result);

            return RedirectToAction("BoxAccessTokenReceived", (object)response);
        }
         **/

        /*
        [HttpGet]
        public ActionResult BoxAccessTokenReceived(object response)
        {
            int userID = WebSecurity.CurrentUserId;

            using (UsersContext db = new UsersContext())
            {
                // Query the database for the row to be updated.
                var token =
                    (from t in db.UserProfiles
                     where t.UserId == userID
                     select t).FirstOrDefault();
                token.DrBxToken = ;
                db.SaveChanges();//Commit the changes to the database
            }
            return View();
        }

         */

/*
      string accessToken = sr.ReadToEnd();
      int userID = WebSecurity.CurrentUserId;
      using (UsersContext db = new UsersContext())
      {
          // Query the database for the row to be updated.
          var token =
              (from t in db.UserProfiles
               where t.UserId == userID
               select t).FirstOrDefault();
          token.BxToken = accessToken;
          db.SaveChanges();//Commit the changes to the database
      }
       */